# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import os

import scrapy
# from itemadapter import ItemAdapter  #TODO:deploy import itemadapter error
from .models.estype import HouseInfoDoc as HIDoc

IMAGES_STORE = '/tmp/img'


class HsmarketPipeline:
    def process_item(self, item, spider):
        return item


class HsmarketEsPipeline:
    def process_item(self, item, spider):
        # adapter = ItemAdapter(item)
        # HIDoc(**adapter.asdict()).save()
        HIDoc(**dict(item)).save()
        return item


class HsmarketImagePipeline:
    async def process_item(self, item, spider):
        request = scrapy.Request(item['image_url'])
        response = await spider.crawler.engine.download(request, spider)

        if response.status != 200:
            # Error happened, return item.
            return item

        # Save screenshot to file, filename will be hash of url.
        filename = os.path.join(IMAGES_STORE, item['file_name'])
        with open(filename, "wb") as f:
            f.write(response.body)

        return item
